<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/21
 * Time: 23:14
 */
namespace app\admin\model\copy;
use think\Exception;

class Copydir
{
    public static function mycopy($from='',$to='',$cover=true){
        if($from==$to){
            return true;
        }
        $to = rtrim($to,'/');
        $from = rtrim($from,'/');
        if(!is_dir($to)){
            try{
                mkdir($to,0777,true);
            }catch (Exception $e){
                $to = strToArray($to,'/');
                $newto = $to[count($to)-1];
                unset($to[count($to)-1]);
                $to = arrayToStr($to);
                if(empty($to)){
                    $to = '/';
                }
                chmod($to,0777);
                mkdir($to.'/'.$newto);
            }
        }
        $array = scandir(rtrim($from,'/'));
        foreach ($array as $value){
            if($value!=='.'&&$value!=='..'){
                if(is_dir($from.'/'.$value)){
//                    echo $to.'/'.$value;exit;
                    Copydir::mycopy($from.'/'.$value,$to.'/'.$value,$cover);
                }else{
                    CopyFile::mycopy($from,$to,$value,$cover);
                }
            }
        }
        return true;

    }
}